package io.modifier.basic.compose.widget.aowsome

import android.content.res.Configuration
import android.graphics.PointF
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.modifier.basic.compose.theme.AppTheme

private val Pink = Color(0xFFEAA8A9)
private val Purple = Color(0xFFD2B4D3)
private val Green = Color(0xFFADD7B9)

private val fakeData = arrayOf(
    PointF(30f, 100f),
    PointF(50f, 30f),
    PointF(70f, 60f),
    PointF(90f, 100f),
    PointF(110f, 30f),
    PointF(130f, 60f),
    PointF(150f, 10f),
    PointF(170f, 20f),
    PointF(180f, 50f),
    PointF(190f, 80f),
    PointF(200f, 60f),
    PointF(210f, 90f),
    PointF(230f, 100f),
    PointF(250f, 30f),
    PointF(260f, 60f),
    PointF(280f, 10f),
    PointF(300f, 90f),
    PointF(320f, 75f),
    PointF(340f, 30f),
    PointF(360f, 10f),
)

@Composable
private fun WaveGraph() {
    val waveColorStops = remember {
        arrayOf(
            0f to Pink,
            0.5f to Purple,
            1f to Green
        )
    }

    Box(
        Modifier
            .fillMaxWidth()
            .height(100.dp)
            .drawWithCache {

                val gradientBrush = Brush.verticalGradient(
                    colorStops = waveColorStops,
                    startY = 0f,
                    endY = size.height
                )

                val path = Path()
                var previousX = 0f
                var previousY = 0f
                fakeData.forEach { point ->
                    val x = point.x.dp.toPx()
                    val y = point.y.dp.toPx()

                    val controlPoint1 = PointF((x + previousX) / 2f, previousY)
                    val controlPoint2 = PointF((x + previousX) / 2f, y)
                    path.cubicTo(
                        controlPoint1.x,
                        controlPoint1.y,
                        controlPoint2.x,
                        controlPoint2.y,
                        x,
                        y
                    )
                    previousX = x
                    previousY = y
                }
                onDrawBehind {
                    drawPath(path, gradientBrush, style = Stroke(10f))
                }
            }
    )
}

@Composable
private fun PreviewLine() {
    AppTheme {
        Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {
            WaveGraph()
        }
    }
}

@Preview(backgroundColor = 0xFF121212, showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Composable
private fun PreviewLightLine() {
    PreviewLine()
}

@Preview(backgroundColor = 0xFFf5f5f5, showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun PreviewDarkLine() {
    PreviewLine()
}